#include <gtest/gtest.h>
#include <chrono>
#include <string>
#include <ctime>
#include <string>
#include "heaptimer.h"

namespace WebServerTest
{
    using namespace std;
    class HeapTimerTest : public ::testing::Test
    {
    protected:
        void SetUp() override
        {
            timer_.clear();
        }
        void TearDown() override
        {
        }
        HeapTimer timer_;
    };


    TEST_F(HeapTimerTest, TestAddTimer)
    {
        // 添加两个定时器，并触发
        auto start_time = chrono::system_clock::now();
        auto node_one=start_time;
        auto node_two=start_time;
        timer_.add(1, 1000, [&node_two]()
                   { node_two = chrono::system_clock::now(); });
        timer_.add(0, 100, [&node_one]()
                   { node_one = chrono::system_clock::now(); });
        
        timer_.tick();
        EXPECT_EQ(node_one, start_time);
        sleep(1);
        timer_.tick();
        EXPECT_LE(start_time + chrono::milliseconds(100), node_one);
    }

    TEST_F(HeapTimerTest, TestHeap){
        //测试堆的正确性
        vector<int> vct{8,1,2,5,6,3,4,7,9,10};
        vector<int> res;
        for(auto &elem : vct){
            timer_.add(elem,elem*100,[&res,elem]{res.push_back(elem);});
        }
        sleep(1.5);
        timer_.tick();
        vector<int> expect{1,2,3,4,5,6,7,8,9,10};
        EXPECT_EQ(res,expect);
    }


};